package com.fqgj.sentry.policy.dao.impl;

import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.sentry.policy.dao.PcRuleDao;
import com.fqgj.sentry.policy.entity.PcRuleEntity;
import com.fqgj.log.interfaces.Log;
import com.fqgj.log.factory.LogFactory;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: yujianfu
 * Date: 2017-07-12
 * Time: 09:37:58.148
 */

@Repository
public class PcRuleDaoImpl extends AbstractBaseMapper<PcRuleEntity> implements PcRuleDao {
    public static final Log LOGGER = LogFactory.getLog(PcRuleDaoImpl.class);

    @Override
    public List<PcRuleEntity> findRuleListByCode(String code) {
        return getSqlSession().selectList(getStatement("findRuleListByCode"), code);

    }

    @Override
    public List<PcRuleEntity> selectListByPolicyId(Long policyId, Integer active, Integer ruleType) {
        Map params = new HashMap();
        params.put("policyId", policyId);
        params.put("ruleType", ruleType);
        params.put("active", Arrays.asList(0, 1).contains(active) ? active : null);
        List<PcRuleEntity> ruleEntityList = getSqlSession().selectList(getStatement("selectListByPolicyId"), params);
        return ruleEntityList;

    }

    @Override
    public List<PcRuleEntity> selectListByPolicyIds(List<Long> policyIds) {
        Map params = new HashMap();
        params.put("policyIds", policyIds);
        List<PcRuleEntity> ruleEntityList = getSqlSession().selectList(getStatement("selectListByPolicyIds"), params);
        return ruleEntityList;
    }

    @Override
    public List<PcRuleEntity> findActiveRuleListByPolicyId(Long policyId, Integer ruleType) {
        Map params = new HashMap();
        params.put("policyId", policyId);
        params.put("ruleType", ruleType);
        List<PcRuleEntity> ruleEntityList = getSqlSession().selectList(getStatement("findActiveRuleListByPolicyId"), params);
        return ruleEntityList;
    }

    @Override
    public Integer deleteRuleById(Long ruleId) {
        return getSqlSession().update(getStatement("deleteRuleById"), ruleId);
    }

    @Override
    public void deleteByRuleIds(List<Long> ruleIds) {
        Map params = new HashMap();
        params.put("ruleIds", ruleIds);
        getSqlSession().selectList(getStatement("deleteByRuleIds"), params);
    }

    @Override
    public Integer updateRuleActive(Long ruleId, Integer activeStatus) {
        Map params = new HashMap();
        params.put("ruleId", ruleId);
        params.put("activeStatus", activeStatus);
        return getSqlSession().update(getStatement("updateRuleActive"), params);
    }

    @Override
    public Integer findMaxCode(Long policySetsId) {
        PcRuleEntity pcRuleEntity = getSqlSession().selectOne(getStatement("findMaxCode"), policySetsId);
        if (pcRuleEntity != null && pcRuleEntity.getCode() != null) {
            return Integer.valueOf(pcRuleEntity.getCode());
        }
        return null;
    }

    @Override
    public Integer findMaxSort(Long policySetsId) {
        PcRuleEntity pcRuleEntity = getSqlSession().selectOne(getStatement("findMaxSort"), policySetsId);
        if (pcRuleEntity != null && pcRuleEntity.getSort() != null) {
            return pcRuleEntity.getSort();
        }
        return null;
    }

    @Override
    public List<PcRuleEntity> findActiveByPolicySetsIdAndExecutorId(Long policySetsId, Long executorId) {
        Map params = new HashMap();
        params.put("policySetsId", policySetsId);
        params.put("executorId", executorId);
        return getSqlSession().selectList(getStatement("findActiveByPolicySetsIdAndExecutorId"), params);
    }

    @Override
    public PcRuleEntity findCommonRuleByPolicy(Long policyId) {
        return getSqlSession().selectOne(getStatement("findCommonRuleByPolicy"), policyId);
    }

    /**
     * 规则 排序
     *
     * @param ruleId
     * @param sort
     * @return
     */
    @Override
    public Boolean updateRuleSort(Long ruleId, Integer sort) {
        Map<String, Object> map = new HashMap<>();
        map.put("ruleId", ruleId);
        map.put("sort", sort);
        return getSqlSession().update(getStatement("updateRuleSort"), map) >= 1;
    }

}